package org.nowcoder.leetcode.BTree;


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * Title  : 429. N-ary Tree Level Order Traversal
 * Source : https://leetcode.cn/problems/n-ary-tree-level-order-traversal/description/
 * Author : XrazYang
 * Date   : 2024-04-09
 */

public class LeetCode_429 {
    private class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public List<List<Integer>> levelOrder(Node root) {
        //边界条件
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> results = new ArrayList<>();
        Queue<Node> que = new ArrayDeque<>();
        que.offer(root);
        while (!que.isEmpty()) {
            int size = que.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node node = que.poll();
                temp.add(node.val);
                for (Node child : node.children) {
                    que.add(child);
                }
            }
            results.add(temp);
        }
        return results;
    }
}
